<?php
/***************************************************************************
 *
 *   copyright            : (C) 2005 Winds of Storm
 *
 *   $Id: report.php,v 1.6 2006/03/03 16:16:13 nathan Exp $
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

$report		= array('TYPE'		=>	'command',
			'NAME'		=>	'Abuse Report',
			'VERSION'	=>	'0.0.1',
			'CREDIT'	=>	'StormeRider',
			'FUNC'		=>	'pluginReport',
			'HELP'		=>	'pluginReportHelp');

$reportEmail	= 'admins@evolutionwriters.com';
$reportFrom	= 'stormerider@gmail.com';

$plugins[]	= $report;

function pluginReportHelp($help) {
	global	$report;

	$help['report1']	= array("/report",
					"Send an abuse report to the moderators." .
					pluginCredit($report));
	$help['report2']	= array("/report [msg]",
					"Send an abuse report to the moderators with the attached [msg]." .
					pluginCredit($report));

	return $help;
}

function pluginReport($userinfo, $event) {
	global	$p_row;
	global	$reportEmail;
	global	$reportFrom;

        $username       = $userinfo['username'];
        $userid         = $userinfo['userid'];
        $isMod          = $userinfo['isMod'];

	$msg	= $event[0]['msg'];
	$room	= $event[0]['room'];
	if ((eregi("^/report", $msg))) {
		$event[0]['parsed']	= 1;
		if (strlen($msg) > 8) {
			$report_msg	= substr($msg, 8);
		} else {
			$report_msg	= '';
		}
		
		$sql	= "(SELECT *,DATE_FORMAT(event_date, '%m/%d %H:%i') AS event_date_fmt
				FROM {$dbprefix}events
				WHERE
					user_id_from=$userid OR
					user_id_to=$userid OR
					room_id_to=$room
				ORDER BY
					event_id DESC
				LIMIT 0,50)
				ORDER BY event_id ASC";
		$result	= $db->sql_query($sql);

		$msg	= "<html><body>$username filed the following report:<br />\r\n";
		if ($report_msg != '') {
			$msg	.= "Notes: $report_msg<br />\r\n";
		}
		$msg	.= "SQL: <pre>$sql</pre>\r\n";
		$msg	.= "<table width=100% border=1>\r\n";
		$msg	.= "<tr><th colspan=3>Reported Chat Events:</th></tr>\r\n";
		$msg	.= "<tr><th>Event ID</th><th>Time</th><th>Text</th></tr>\r\n";
		while ($rrow = $db->sql_fetchrow($result)) {
			$msg	.= "<tr><td>{$rrow['event_id']}</td>\r\n";
			$msg	.= "<td>{$rrow['event_date_fmt']}</td>\r\n";
			$msg	.= "<td>" . wordwrap(stripslashes($rrow['event_text'])) . "</td></tr>\r\n";
		}
		$msg	.= "</table></body></html>\r\n";

		$subj	= '[Evolution Writers] Chat Incident Report';
		$hdrs	= "From: $reportFrom\r\n" .
			  "Reply-To: $reportFrom\r\n" .
			  'Content-type: text/html' . "\r\n" .
			  'X-Mailer: PHP/' . phpversion();

		mail($reportEmail, $subj, $msg, $hdrs,'-f' . $reportFrom);

		$event[0]['msg']	= "<span class=sysmsg>*** Chat incident report sent to the administration.</span>";
		$event[0]['room']	= 0;
		$event[0]['userto']	= $userid;
	}
}
?>
